﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Model;
using IPR_StudyMeetingMrgSys.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace IPR_WebApi.Controllers
{
    public class AttendanceController : ApiController
    {
        [HttpGet]
        [Route("api/Attendance/GetAttendanceCount")]
        public IHttpActionResult GetAttendanceCount(string swhere, int page, int limit)
        {
            swhere = swhere ?? string.Empty;
            int startindex = (page - 1) * limit + 1;
            int endindex = page * limit;
            DateTime startTime = DateTime.Now;
            DateTime endTime = DateTime.Now;
            string chedui_Ids= "";
            string UserID = "";
            if (!string.IsNullOrWhiteSpace(swhere))
            {
                Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                foreach (var item in jsonObject)
                {
                    if (item.Key == "StartDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            startTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "EndDateTime")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            endTime = Convert.ToDateTime(item.Value);
                        }
                    }
                    if (item.Key == "chedui_Ids")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            chedui_Ids = item.Value;
                        }
                    }
                    if (item.Key == "UserID")
                    {
                        if ((item.Value != "") && (item.Value != null))
                        {
                            UserID = item.Value;
                        }
                    }

                }
            }
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.Attendance().
                GetAttendanceCount(chedui_Ids, startTime, endTime, UserID);
                if (dsSearch != null)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dsSearch.Tables[0];
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }


        /// <summary>
        /// 根据出勤时间获取出勤的日期做为查询的表头
        /// </summary>
        /// <param name="StartDateTime"></param>
        /// <param name="EndDateTime"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/Attendance/GetAttendanceDays")]
        public IHttpActionResult GetAttendanceDays(string StartDateTime,string EndDateTime)
        {
            DataTable dt = new DataTable("TabName");
            dt.Columns.Add("field");
            dt.Columns.Add("title");

            ResultMsg resultMsg = new ResultMsg();
            try
            {
                string sDay = "";
                DateTime dt1 = Convert.ToDateTime(StartDateTime);
                DateTime dt2 = Convert.ToDateTime(EndDateTime);
                DataRow myDRother = dt.NewRow();
                myDRother = dt.NewRow();
                myDRother[0] = "UserID";
                myDRother[1] = "工号";
                dt.Rows.Add(myDRother);

                myDRother = dt.NewRow();
                myDRother[0] = "UserName";
                myDRother[1] = "用户名";
                dt.Rows.Add(myDRother);

                myDRother = dt.NewRow();
                myDRother[0] = "Post";
                myDRother[1] = "职名";
                dt.Rows.Add(myDRother);

                while (dt1 <= dt2)
                {
                    sDay = dt1.ToString("MM-dd");
                    DataRow myDR = dt.NewRow();
                    myDR[0] = sDay;
                    myDR[1] = sDay;
                    dt.Rows.Add(myDR);

                    dt1 = dt1.AddDays(1);
                }
                myDRother = dt.NewRow();
                myDRother[0] = "TotalNum";
                myDRother[1] = "合计";
                dt.Rows.Add(myDRother);
                //返回token信息
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = dt;
                 
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }
    }
}
